package com.jbp.controller;
import	java.util.HashMap;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.model.*;
import com.jbp.service.ArticleService;
import com.jbp.service.UserRelationService;
import com.jbp.service.UserService;
import com.jbp.service.VisitorLogService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;

/**
 * 用户关系(UserRelation)表控制层
 * @author cc
 * @since 2020-06-21 12:06:02
 */
@Api(tags ="APP接口--用户关系" )
@ApiIgnore
@RestController
@RequestMapping("rest/userRelation")
public class UserRelationController {
    @Autowired
    private UserRelationService userRelationService;
    @Autowired
    private UserService userService;
    @Autowired
    private VisitorLogService visitorLogService;
    @Autowired
    private ArticleService articleService;

    ///用户搜索
    @ApiOperation(value="用户搜索", notes ="传 name")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = User.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/searchUser")
    public R searchUser(@RequestParam Map<String, Object> params,@RequestAttribute("userId") Integer userId){
        params.put("userId",userId);
        PageUtils page = userService.searchUser(params);

        return R.ok(page);
    }

    ///好友列表
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = UserRelation.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiOperation(value="列表", notes ="传 status 1好友 2 关注 3 粉丝, isOnline 在线状态 online在线, name 模糊搜索字段")
    @GetMapping("/friendList")
    public R friendList(@RequestParam Map<String, Object> params,@RequestAttribute("userId") Integer userId){
        params.put("userId",userId);
        PageUtils page = userRelationService.friendList(params);

        return R.ok(page);
    }

    ///关注
    @ApiOperation(value="关注")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "friendId",value = "好友id", paramType = "body"),
    })
    @PostMapping("/follow")
    public R follow(@RequestAttribute("userId")Integer userId, @RequestBody UserRelation req){
        if (req.getFriendId()==null) {
            return R.error("参数异常");
        }

        userRelationService.follow(userId, req.getFriendId());
        return R.ok();
    }

    ///取消关注
    @ApiOperation(value="取消关注")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "friendId",value = "好友id", paramType = "body"),
    })
    @PostMapping("/unFollow")
    public R unFollow(@RequestAttribute("userId")Integer userId, @RequestBody UserRelation req){
        if (req.getFriendId()==null) {
            return R.error("参数异常");
        }

        userRelationService.unFollow(userId, req.getFriendId());
        return R.ok();
    }

    ///拉黑/取消拉黑
    @ApiOperation(value="拉黑/取消拉黑")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "friendId",value = "好友id", paramType = "body"),
            @ApiImplicitParam(name = "black",value = "是否拉黑 0否， 1是", paramType = "body"),
    })
    @PostMapping("/lahei")
    public R lahei(@RequestAttribute("userId")Integer userId, @RequestBody UserRelation req){
        if (req.getFriendId()==null || req.getBlack()==null) {
            return R.error("参数异常");
        }

        userRelationService.lahei(userId, req);
        return R.ok();
    }

    ///黑名单列表
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = UserRelation.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiOperation(value="黑名单列表")
    @GetMapping("/blackList")
    public R blackList(@RequestParam Map<String, Object> params,@RequestAttribute("userId") Integer userId){
        params.put("userId",userId);
        PageUtils page = userRelationService.blackList(params);

        return R.ok(page);
    }

    ///好友统计
    @ApiOperation(value="好友统计")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Map.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/friendCount")
    public R friendCount(@RequestAttribute("userId")Integer userId){
        //关注
        int gz = userRelationService.count(new QueryWrapper<UserRelation>().eq("user_id", userId).in("status", 1, 2));
        //粉丝
        int fs = userRelationService.count(new QueryWrapper<UserRelation>().eq("user_id", userId).in("status", 1, 3));
        //好友
        int hy = userRelationService.count(new QueryWrapper<UserRelation>().eq("user_id", userId).eq("status", 1));

        //访问数
        int vistor = visitorLogService.count(new QueryWrapper<VisitorLog>().eq("user_id", userId));

        int whoVisitMe = visitorLogService.count(new QueryWrapper<VisitorLog>().eq("friend_id", userId));

        //发布动态数
        int article = articleService.count(new QueryWrapper<Article>().eq("author_id", userId).ne("status","del"));

        Map<String, Integer>  map = new HashMap<> ();
        map.put("gz", gz);
        map.put("fs", fs);
        map.put("hy", hy);
        map.put("vistor", vistor);
        map.put("whoVisitMe", whoVisitMe);
        map.put("article", article);

        return R.ok(map);
    }
}